home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / Source / Chapter 11 / Engine / SceneManager.h < prev    next >
Encoding:
C/C++ Source or Header  |  2004-10-01  |  8.6 KB  |  213 lines

  1. //-----------------------------------------------------------------------------
  2. // Manages and renders a scene and the objects in it using frustum and
  3. // occlusion culling, and performing collision detection.
  4. //
  5. // Programming a Multiplayer First Person Shooter in DirectX
  6. // Copyright (c) 2004 Vaughan Young
  7. //-----------------------------------------------------------------------------
  8. #ifndef SCENE_MANAGER_H
  9. #define SCENE_MANAGER_H
  10.  
  11. //-----------------------------------------------------------------------------
  12. // Scene Occluder Structure
  13. //-----------------------------------------------------------------------------
  14. struct SceneOccluder : public BoundingVolume
  15. {
  16.     unsigned long visibleStamp; // Stamp indicating if the occluder is visible in the current frame.
  17.     D3DXVECTOR3 translation; // Translation of the occluder.
  18.     unsigned long totalFaces; // Total number of faces in the occluder's mesh.
  19.     Vertex *vertices; // Array containing the occluder's vertices transformed into world space.
  20.     unsigned short *indices; // Array of indices into the vertex array.
  21.     LinkedList< D3DXPLANE > *planes; // List of planes that define the occluded volume.
  22.     float distance; // Distance between the viewer and the occluder.
  23.  
  24.     //-------------------------------------------------------------------------
  25.     // The scene occluder structure constructor.
  26.     //-------------------------------------------------------------------------
  27.     SceneOccluder( D3DXVECTOR3 t, ID3DXMesh *mesh, D3DXMATRIX *world )
  28.     {
  29.         // Clear the visible stamp.
  30.         visibleStamp = -1;
  31.  
  32.         // Set the translation.
  33.         translation = t;
  34.  
  35.         // Set the total faces and create the vertx and idex arrays.
  36.         totalFaces = mesh->GetNumFaces();
  37.         vertices = new Vertex[mesh->GetNumVertices()];
  38.         indices = new unsigned short[totalFaces * 3];
  39.  
  40.         // Lock the given mesh's vertex and index buffers.
  41.         Vertex* verticesPtr;
  42.         mesh->LockVertexBuffer( 0, (void**)&verticesPtr );
  43.         unsigned short *indicesPtr;
  44.         mesh->LockIndexBuffer( 0, (void**)&indicesPtr );
  45.  
  46.         // Copy the vertices and the indices.
  47.         memcpy( vertices, verticesPtr, VERTEX_FVF_SIZE * mesh->GetNumVertices() );
  48.         memcpy( indices, indicesPtr, sizeof( unsigned short ) * totalFaces * 3 );
  49.  
  50.         // Unlock the vertex and index buffers.
  51.         mesh->UnlockVertexBuffer();
  52.         mesh->UnlockIndexBuffer();
  53.  
  54.         // Transform the vertices into world space.
  55.         for( unsigned long v = 0; v < mesh->GetNumVertices(); v++ )
  56.             D3DXVec3TransformCoord( &vertices[v].translation, &vertices[v].translation, world );
  57.  
  58.         // Create a list of planes for building the occlusion volume.
  59.         planes = new LinkedList< D3DXPLANE >;
  60.  
  61.         // Create a bounding volume from the occluder's mesh.
  62.         BoundingVolumeFromMesh( mesh );
  63.  
  64.         // Resposition the bounding volume into world space.
  65.         D3DXMATRIX location;
  66.         D3DXMatrixTranslation( &location, t.x, t.y, t.z );
  67.         RepositionBoundingVolume( &location );
  68.     }
  69.  
  70.     //-------------------------------------------------------------------------
  71.     // The scene occluder structure destructor.
  72.     //-------------------------------------------------------------------------
  73.     virtual ~SceneOccluder()
  74.     {
  75.         SAFE_DELETE_ARRAY( vertices );
  76.         SAFE_DELETE_ARRAY( indices );
  77.  
  78.         SAFE_DELETE( planes );
  79.     }
  80. };
  81.  
  82. //-----------------------------------------------------------------------------
  83. // Scene Leaf Structure
  84. //-----------------------------------------------------------------------------
  85. struct SceneLeaf : public BoundingVolume
  86. {
  87.     SceneLeaf *children[8]; // Array of child scene leaf pointers.
  88.     unsigned long visibleStamp; // Indicates if the scene leaf is visible in the current frame.
  89.     LinkedList< SceneOccluder > *occluders; // list of scene occluders in the scene leaf.
  90.     unsigned long totalFaces; // Total number of faces in the scene leaf.
  91.     unsigned long *faces; // Array of indices pointing to the faces in the scene leaf.
  92.  
  93.     //-------------------------------------------------------------------------
  94.     // The scene leaf structure constructor.
  95.     //-------------------------------------------------------------------------
  96.     SceneLeaf()
  97.     {
  98.         for( char l = 0; l < 8; l++ )
  99.             children[l] = NULL;
  100.         occluders = new LinkedList< SceneOccluder >;
  101.         totalFaces = 0;
  102.         faces = NULL;
  103.     }
  104.  
  105.     //-------------------------------------------------------------------------
  106.     // The scene leaf structure destructor.
  107.     //-------------------------------------------------------------------------
  108.     virtual ~SceneLeaf()
  109.     {
  110.         for( char l = 0; l < 8; l++ )
  111.             SAFE_DELETE( children[l] );
  112.         occluders->ClearPointers();
  113.         SAFE_DELETE( occluders );
  114.         SAFE_DELETE_ARRAY( faces );
  115.     }
  116. };
  117.  
  118. //-----------------------------------------------------------------------------
  119. // Scene Face Structure
  120. //-----------------------------------------------------------------------------
  121. struct SceneFace : public IndexedFace
  122. {
  123.     RenderCache *renderCache; // Pointer to the render cache this face belongs to.
  124.     unsigned long renderStamp; // Indicates when the face was last rendered.
  125. };
  126.  
  127. //-----------------------------------------------------------------------------
  128. // Ray Intersection Result Struction
  129. //-----------------------------------------------------------------------------
  130. struct RayIntersectionResult
  131. {
  132.     Material *material; // Pointer to the material of the intersected face.
  133.     float distance; // Distance the ray can travel until intersection occurs.
  134.     D3DXVECTOR3 point; // Intersection point in 3D space.
  135.     SceneObject *hitObject; // Pointer to the hit scene object (if one was hit).
  136.  
  137.     //-------------------------------------------------------------------------
  138.     // The ray intersection result structure constructor.
  139.     //-------------------------------------------------------------------------
  140.     RayIntersectionResult()
  141.     {
  142.         material = NULL;
  143.         distance = 0.0f;
  144.         point = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
  145.         hitObject = NULL;
  146.     }
  147. };
  148.  
  149. //-----------------------------------------------------------------------------
  150. // Scene Manager Class
  151. //-----------------------------------------------------------------------------
  152. class SceneManager
  153. {
  154. public:
  155.     SceneManager( float scale, char *spawnerPath );
  156.     virtual ~SceneManager();
  157.  
  158.     void LoadScene( char *name, char *path = "./" );
  159.     void DestroyScene();
  160.     bool IsLoaded();
  161.  
  162.     void Update( float elapsed, D3DXMATRIX *view = NULL );
  163.     void Render( float elapsed, D3DXVECTOR3 viewer );
  164.  
  165.     SceneObject *AddObject( SceneObject *object );
  166.     void RemoveObject( SceneObject **object );
  167.  
  168.     SceneObject *GetRandomPlayerSpawnPoint();
  169.     SceneObject *GetSpawnPointByID( long id );
  170.     long GetSpawnPointID( SceneObject *point );
  171.  
  172.     LinkedList< SpawnerObject > *GetSpawnerObjectList();
  173.  
  174.     bool RayIntersectScene( RayIntersectionResult *result, D3DXVECTOR3 rayPosition, D3DXVECTOR3 rayDirection, bool checkScene = true, SceneObject *thisObject = NULL, bool checkObjects = false );
  175.  
  176. private:
  177.     void BuildOcclusionVolume( SceneOccluder *occluder, D3DXVECTOR3 viewer );
  178.  
  179.     void RecursiveSceneBuild( SceneLeaf *leaf, D3DXVECTOR3 translation, float halfSize );
  180.     bool RecursiveSceneFrustumCheck( SceneLeaf *leaf, D3DXVECTOR3 viewer );
  181.     void RecursiveSceneOcclusionCheck( SceneLeaf *leaf );
  182.  
  183. private:
  184.     char *m_name; // Name of the scene.
  185.     float m_scale; // Scene scale in meters/unit.
  186.     ViewFrustum m_viewFrustum; // View frustum for visiblity culling.
  187.     D3DXVECTOR3 m_gravity; // Constant gravity pull.
  188.     bool m_loaded; // Indicates if the scene has been loaded or not.
  189.     Mesh *m_mesh; // Mesh for the scene.
  190.     unsigned long m_maxFaces; // Maximum number of faces per scene leaf.
  191.     float m_maxHalfSize; // Maximum half size of a scene leaf.
  192.     unsigned long m_frameStamp; // Current frame time stamp.
  193.  
  194.     LinkedList< SceneObject > *m_dynamicObjects; // Linked list of dynamic objects.
  195.     LinkedList< SceneOccluder > *m_occludingObjects; // Linked list of occluding objects.
  196.     LinkedList< SceneOccluder > *m_visibleOccluders; // Linked list of visible occluders each frame.
  197.     LinkedList< SceneObject > *m_playerSpawnPoints; // Linked list of player spawn points.
  198.     LinkedList< SpawnerObject > *m_objectSpawners; // Linked list of object spawners.
  199.     char *m_spawnerPath; // Path used for loading the spawner object scripts.
  200.  
  201.     SceneLeaf *m_firstLeaf; // The first scene leaf in the scene hierarchy.
  202.  
  203.     IDirect3DVertexBuffer9 *m_sceneVertexBuffer; // Vertex buffer for all the vertices in the scene.
  204.     Vertex *m_vertices; // Pointer for accessing the vertices in the vertex buffer.
  205.     unsigned long m_totalVertices; // Total number of vertices in the scene.
  206.  
  207.     LinkedList< RenderCache > *m_renderCaches; // Linked list of render caches.
  208.  
  209.     unsigned long m_totalFaces; // Total number of faces in the scene.
  210.     SceneFace *m_faces; // Array of faces in the scene.
  211. };
  212.  
  213. #endif